package com.inxedu.os.edu.service.emil.impl;

import com.inxedu.os.edu.dao.EduEmailsendHistoryMapper;

import com.inxedu.os.edu.dao.EduUserMapper;
import com.inxedu.os.edu.dao.SysUserMapper;
import com.inxedu.os.edu.pojo.*;
import com.inxedu.os.edu.service.emil.AdminEmailMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class AdminEmailMsgImpl implements AdminEmailMsg {
    @Autowired
    private EduEmailsendHistoryMapper emailmapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private EduUserMapper userMapper;
//    @Override
//    public List<EduEmailsendHistoryAndLoginname> selectAll() {
//        List<EduEmailsendHistoryAndLoginname> selectAll = emailmapper.selectAll();
//        return selectAll;
//    }
    //查询邮件管理
    @Override
    public List<EduEmailsendHistoryAndLoginname> GetEmailByExample(UserEmailMsg userEmailMsg) {
        List<EduEmailsendHistoryAndLoginname> eduEmailsendHistoryAndLoginnames = emailmapper.selectBykey(userEmailMsg);
        return eduEmailsendHistoryAndLoginnames;
    }
    //发送邮件
    @Override
    public EduEmailsendHistoryAndLoginname GetEmailByid(Integer id) {
        EduEmailsendHistoryWithBLOBs eduemil = emailmapper.selectByPrimaryKey(id);
        Integer userId = eduemil.getUserId();
        SysUser user = sysUserMapper.selectByPrimaryKey(userId);
        String loginName = user.getLoginName();
        EduEmailsendHistoryAndLoginname edu=new EduEmailsendHistoryAndLoginname();
        edu.setLoginName(loginName);
        edu.setContent(eduemil.getContent());
        edu.setCreateTime(eduemil.getCreateTime());
        edu.setEmail(eduemil.getEmail());
        edu.setId(id);
        edu.setStatus(eduemil.getStatus());
        edu.setSendTime(eduemil.getSendTime());
        edu.setTitle(eduemil.getTitle());
        edu.setType(eduemil.getType());
        edu.setUserId(userId);
        return edu;
    }
    //发送邮件
    @Override
    public int addEmail(EduEmailsendHistoryWithBLOBs bloBs) {
        int insert = emailmapper.insert(bloBs);
        return insert;
    }

    @Override
    public List<EduUser> getUserList() {
        EduUserExample example = new EduUserExample();
     /*   EduUserExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdIsNotNull();*/
        List<EduUser> users = userMapper.selectByExample(example);
        return users;
    }

    @Override
    public Map<String, Object> checkEmail(String emailStr) {
        Map<String, Object> returnMap = new HashMap<String, Object>();
        emailStr = emailStr.replaceAll("\r\n", "");// 去除空格回车
        emailStr = emailStr.replaceAll(" ", "");// 去除空格回车
        String[] lm = emailStr.split(",");// 定义数组

        List list = new ArrayList();// new一个arralist
        Set set = new HashSet();// new 一个hashset
        set.addAll(Arrays.asList(lm));// 将数组转为list并存入set中，就可以去掉重复项了
        for (Iterator it = set.iterator(); it.hasNext();) {
            list.add(it.next());// 遍历set 将所有元素键入list中
        }
        String noRepeatList = list.toString();
        noRepeatList = noRepeatList.replace("[", "");
        noRepeatList = noRepeatList.replace("]", "");
        noRepeatList = noRepeatList.replace(" ", "");
        noRepeatList = noRepeatList.trim();

        boolean flag = true;
        String errorMessage = "";
        String[] lms = noRepeatList.split(",");
        if (lms.length > 0) {
            for (int i = 0; i < lms.length; i++) {
                if (!lms[i].trim().matches("^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$")) {
                    flag = false;
                    errorMessage = lms[i] + "格式有误";
                    break;
                }
            }
        }

        returnMap.put("flag", flag);
        returnMap.put("returnList", noRepeatList);
        returnMap.put("errorMessage", errorMessage);
        return returnMap;
    }
}

